package org.example;

import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import javax.swing.*;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import static java.lang.Thread.sleep;

public class Main {
    public static void main(String[] args) throws InterruptedException {

        //xpath选择器
//        test01();
        //css选择器
//        test02();
        //clear
//        test03();
        //隐式等待（tagName不会使用）
//        test04();
        //显示等待1
//        test05();
        //显示等待2
//        test06();
        //获取元素的属性值
//        test07();
        //获取title
//        test08();
        //获取url
//        test09();
        //浏览器最大化
//        test10();
        //设置浏览器的大小
//        test11();
        //浏览器的前进，后退
//        test12();
        //操作浏览器的滚动条(没有效果)
//        test13();
        //键盘事件
//        test14();
        //鼠标操作
//        test15();
        //切换窗口
//        test16();
    }

    private static void test16() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com/");
        Actions actions = new Actions(webDriver);
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("崩坏三");
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(3000);
        WebElement webElement = webDriver.findElement(By.cssSelector("#\\32  > div > div.gameinfo_2nxq4 > div:nth-child(1) > h3 > a:nth-child(1)"));
        actions.moveToElement(webElement).click().perform();
        Set<String> all_handles = webDriver.getWindowHandles();
        for (String tmp : all_handles){
            webDriver.switchTo().window(tmp);
            sleep(3000);
        }
        webDriver.quit();
    }

    private static void test15() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com/");
        Actions action = new Actions(webDriver);
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("崩坏三");
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(3000);
        WebElement target = webDriver.findElement(By.cssSelector("#\\32  > div > div.gameinfo_2nxq4 > div:nth-child(1) > h3 > a:nth-child(1)"));
        //右击
        //action.moveToElement(target).contextClick().perform();
        //click只是写入一个点击动作，真正执行还要使用perform
        action.moveToElement(target).click().perform();
        Set<String> all_handles = webDriver.getWindowHandles();
        for (String tmp : all_handles){
            webDriver.switchTo().window(tmp);
            sleep(3000);
        }
        webDriver.quit();
    }

    private static void test14() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com/");
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("原神");
        //webDriver.findElement(By.cssSelector("#su")).click();
        //enter键
        //webDriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.ENTER);
        //ctrl + A
        webDriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL, "A");
        //ctrl + X
        sleep(3000);
        webDriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL, "X");
        sleep(3000);
        webDriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL, "V");
        //enter键
        webDriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.ENTER);
        sleep(3000);
        webDriver.quit();
    }

    private static void test13() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com/");
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("布洛妮娅");
        webDriver.findElement(By.cssSelector("#su")).click();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        ((JavascriptExecutor)webDriver).executeScript("document.documentElement.scrollTop=10000");//滚动条在最下面
        sleep(3000);
        ((JavascriptExecutor)webDriver).executeScript("document.documentElement.scrollTop=0");//滚动条在最上面
        sleep(3000);
        webDriver.quit();
    }

    private static void test12() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://baidu.com/");
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("符玄");
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(3000);
        webDriver.navigate().back();//回退
        sleep(3000);
        webDriver.navigate().forward();//前进
        sleep(3000);
        webDriver.quit();
    }

    private static void test11() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://baidu.com/");
        webDriver.manage().window().setSize(new Dimension(200, 200));//设置浏览器的大小
        sleep(3000);
        webDriver.quit();
    }

    private static void test10() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com/");
        webDriver.manage().window().maximize();//浏览器最大化
        sleep(3000);
        webDriver.quit();
    }

    private static void test09() {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com/");
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("星穹铁道");
        webDriver.findElement(By.cssSelector("#su")).click();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        String url = webDriver.getCurrentUrl();
        System.out.println(url);
        if(url.equals("https://www.baidu.com/")){
            System.out.println("测试失败");
        }else {
            System.out.println("测试成功");
        }
        webDriver.quit();
    }

    private static void test08() {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com/");
        String expTitle = "百度一下，你就知道";
        String curTitle = webDriver.getTitle();
        System.out.println(curTitle);
        if(curTitle.equals(expTitle)){
            System.out.println("测试通过");
        }else {
            System.out.println("测试不通过");
        }
        webDriver.quit();
    }

    private static void test07() {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com/");
        WebElement baidu_button = webDriver.findElement(By.cssSelector("#su"));
        String text = "百度一下";
        if (baidu_button.getAttribute("value").equals(text)){
            System.out.println("测试通过");
        }else {
            System.out.println("测试不通过");
        }
        webDriver.quit();
    }

    private static void test06() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com/");
        WebDriverWait webDriverWait = new WebDriverWait(webDriver, 3);
        webDriverWait.until(ExpectedConditions.textToBe(By.cssSelector("#s-top-left > a:nth-child(1)"), "新闻"));
        sleep(3000);
        webDriver.quit();
    }

    private static void test05() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com/");
        WebDriverWait webDriverWait = new WebDriverWait(webDriver, 3);
        webDriverWait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#s-top-left > a:nth-child(1)"))).click();
        String url = "https://www.baidu.com/";
        if(webDriver.getCurrentUrl().equals(url)){
            System.out.println("测试通过");
        }else {
            System.out.println("测试不通过");
        }
        webDriver.quit();
    }

    private static void test04() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com");
        WebElement search_input = webDriver.findElement(By.cssSelector("#kw"));
        //search_input.sendKeys("菲谢尔");
        search_input.sendKeys("星穹铁道");
        webDriver.findElement(By.cssSelector("#su")).click();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //List<WebElement> results = webDriver.findElements(By.tagName("a em"));
        List<WebElement> results = webDriver.findElements(By.xpath("//*[@id=\"3001\"]/div/div[1]/div/div/h3/div/a/font"));
        int flag = 0;
        for (int i = 0; i < results.size(); i++){
            if(results.get(i).getText().equals("星穹铁道")){
                flag = 1;
                break;
            }
        }
        if(flag == 1){
            System.out.println("测试通过");
        }else {
            System.out.println("测试不通过");
        }

        webDriver.quit();

    }

    private static void test03() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com/");
        WebElement search_input = webDriver.findElement(By.cssSelector("#kw"));
        search_input.sendKeys("张三");
        sleep(3000);
        search_input.clear();
        search_input.sendKeys("原神");
        sleep(3000);
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(3000);
        List<WebElement> results = webDriver.findElements(By.xpath("//*[@id=\"3001\"]/div/div[1]/div/div/h3/div/a/font"));
        int flag = 0;
        for(int i = 0; i < results.size(); i++){
            if(results.get(i).getText().equals("原神")){
                System.out.println("测试通过");
                flag = 1;
                break;
            }
        }
        if(flag == 0){
            System.out.println("测试不通过");
        }
        webDriver.quit();

    }

    private static void test02() throws InterruptedException {
        //创建一个驱动
        WebDriver webDriver = new ChromeDriver();
        //打开百度网页
        webDriver.get("https://www.baidu.com/");
        //找到搜索框
        WebElement search_input = webDriver.findElement(By.cssSelector("#kw"));
        //输入要搜索的词
        search_input.sendKeys("星穹铁道");
        //点击百度一下
        webDriver.findElement(By.cssSelector("#su")).click();
        //判断是否为搜索到
        sleep(3000);
        String unexpectUrl = "https://www.baidu.com/";
        String curUrl = webDriver.getCurrentUrl();
        if(curUrl.equals(unexpectUrl)){
            System.out.println("没有跳转页面,测试不通过");
        }
        int flag = 0;
        List<WebElement> results = webDriver.findElements(By.xpath("//*[@id=\"3001\"]/div/div[1]/div/div/h3/div/a/font"));
        for (int i = 0; i < results.size(); i++){
            if(results.get(i).getText().equals("星穹铁道")){
                flag = 1;
                break;
            }
        }
        if (flag == 1){
            System.out.println("测试通过,找到星穹铁道");
        }else {
            System.out.println("测试通过,找不到星穹铁道");
        }

        webDriver.quit();

    }

    public static void test01() throws InterruptedException {
        //创建一个驱动
        WebDriver webDriver = new ChromeDriver();
        //打开百度网页
        webDriver.get("https://www.baidu.com/");
        //找到搜索框
        WebElement search_input = webDriver.findElement(By.cssSelector(".s_ipt"));
        //输入搜索内容
        search_input.sendKeys("软件测试");
        //点击确认搜索
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(3000);
        List<WebElement> search_results = webDriver.findElements(By.xpath("//font[text()=\"软件测试\"]"));

        int flag = 0;
        for (int i = 0; i < search_results.size(); i++){
            if (search_results.get(i).getText().equals("软件测试")){
                System.out.println("测试通过");
                flag = 1;
                break;
            }
        }

        if(flag == 0){
            System.out.println("测试不通过");
        }


        webDriver.quit();
    }
}